package com.jeeplus.test.jlnu.student.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.baomidou.mybatisplus.extension.conditions.update.LambdaUpdateChainWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.jeeplus.test.jlnu.student.domain.entity.BsssStudentGraduate;
import com.jeeplus.test.jlnu.student.domain.entity.StudentInfoApprove;
import com.jeeplus.test.jlnu.student.domain.vo.BsssStudentGraduateVo;
import com.jeeplus.test.jlnu.student.dto.VerifyResponse;
import com.jeeplus.test.jlnu.student.mapper.BsssStudentGraduateMapper;
import com.jeeplus.test.jlnu.student.mapper.BsssStudentInfoMapper;
import com.jeeplus.test.jlnu.student.mapper.StudentInfoApproveMapper;
import com.jeeplus.test.jlnu.student.service.BsssStudentGraduateService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Optional;

@Service
public class BsssStudentGraduateServiceImpl extends ServiceImpl<BsssStudentGraduateMapper, BsssStudentGraduate> implements BsssStudentGraduateService {
    @Autowired
    private  BsssStudentGraduateMapper bsssStudentGraduateMapper;

    @Autowired
    StudentInfoApproveMapper studentInfoApproveMapper;


    @Override
    public BsssStudentGraduate getBsssStudentGraduate(String studentId, String flag) {
        LambdaQueryChainWrapper<BsssStudentGraduate> wrapper = new LambdaQueryChainWrapper<>(bsssStudentGraduateMapper);
        wrapper.eq(BsssStudentGraduate::getStudentId,studentId);
        wrapper.eq(BsssStudentGraduate::getFlag,flag);
        return wrapper.one();
    }

    @Override
    public Boolean bsssStudentGraduateUpdate(BsssStudentGraduateVo bsssStudentGraduate) {
        LambdaUpdateChainWrapper<BsssStudentGraduate> wrapper = new LambdaUpdateChainWrapper<>(bsssStudentGraduateMapper);


        if (ObjectUtil.isNotEmpty(bsssStudentGraduate.getGraduationDate())){
            wrapper.set(BsssStudentGraduate::getGraduationDate,bsssStudentGraduate.getGraduationDate());
        }if (ObjectUtil.isNotEmpty(bsssStudentGraduate.getGraduationDestination())){
            wrapper.set(BsssStudentGraduate::getGraduationDestination,bsssStudentGraduate.getGraduationDestination());
        }if (ObjectUtil.isNotEmpty(bsssStudentGraduate.getEducationLevel())){
            wrapper.set(BsssStudentGraduate::getEducationLevel,bsssStudentGraduate.getEducationLevel());
        }if (ObjectUtil.isNotEmpty(bsssStudentGraduate.getSupervisorNameAndTitle())){
            wrapper.set(BsssStudentGraduate::getSupervisorNameAndTitle,bsssStudentGraduate.getSupervisorNameAndTitle());
        }if (ObjectUtil.isNotEmpty(bsssStudentGraduate.getThesisTitle())){
            wrapper.set(BsssStudentGraduate::getThesisTitle,bsssStudentGraduate.getThesisTitle());
        }if (ObjectUtil.isNotEmpty(bsssStudentGraduate.getIsOverseasChinese())){
            wrapper.set(BsssStudentGraduate::getIsOverseasChinese,bsssStudentGraduate.getIsOverseasChinese());
        }if (ObjectUtil.isNotEmpty(bsssStudentGraduate.getOverseasLocation())){
            wrapper.set(BsssStudentGraduate::getOverseasLocation,bsssStudentGraduate.getOverseasLocation());
        }if (ObjectUtil.isNotEmpty(bsssStudentGraduate.getForeignLanguage())){
            wrapper.set(BsssStudentGraduate::getForeignLanguage,bsssStudentGraduate.getForeignLanguage());
        }if (ObjectUtil.isNotEmpty(bsssStudentGraduate.getDegreeObtained())){
            wrapper.set(BsssStudentGraduate::getDegreeObtained,bsssStudentGraduate.getDegreeObtained());
        }if (ObjectUtil.isNotEmpty(bsssStudentGraduate.getPersonalEvaluation())){
            wrapper.set(BsssStudentGraduate::getPersonalEvaluation,bsssStudentGraduate.getPersonalEvaluation());
        }
        if (ObjectUtil.isNotEmpty(bsssStudentGraduate.getGraduationDesign())){
            wrapper.set(BsssStudentGraduate::getGraduationDesign,bsssStudentGraduate.getGraduationDesign());
        }
        if (ObjectUtil.isNotEmpty(bsssStudentGraduate.getOther())){
            wrapper.set(BsssStudentGraduate::getOther,bsssStudentGraduate.getOther());
        }
        if (ObjectUtil.isNotEmpty(bsssStudentGraduate.getLearningContent())){
            wrapper.set(BsssStudentGraduate::getLearningContent,bsssStudentGraduate.getLearningContent());
        }
        if (ObjectUtil.isNotEmpty(bsssStudentGraduate.getLearningSituation())){
            wrapper.set(BsssStudentGraduate::getLearningSituation,bsssStudentGraduate.getLearningSituation());
        }
        if (ObjectUtil.isNotEmpty(bsssStudentGraduate.getPracticeUnit())){
            wrapper.set(BsssStudentGraduate::getPracticeUnit,bsssStudentGraduate.getPracticeUnit());
        }
        if (ObjectUtil.isNotEmpty(bsssStudentGraduate.getDegreeOfEducation())){
            wrapper.set(BsssStudentGraduate::getDegreeOfEducation,bsssStudentGraduate.getDegreeOfEducation());
        }
        LambdaQueryChainWrapper<StudentInfoApprove> queryWrapper =
                new LambdaQueryChainWrapper<>(studentInfoApproveMapper);

        LambdaQueryChainWrapper<BsssStudentGraduate> query =
                new LambdaQueryChainWrapper<>(bsssStudentGraduateMapper);


        // 查询是否存在记录
        Optional<StudentInfoApprove> info = queryWrapper
                .eq(StudentInfoApprove::getStudentId, bsssStudentGraduate.getStudentId())
                .eq(StudentInfoApprove::getOutsideType, "2")
                .oneOpt();

        Optional<BsssStudentGraduate> graduate = query
                .eq(BsssStudentGraduate::getStudentId,bsssStudentGraduate.getStudentId())
                .oneOpt();


        // 生成 VerifyResponse 的 JSON 数据
        String data = generateVerifyResponseJson(bsssStudentGraduate);



        // 根据查询结果执行更新或插入
        if (info.isPresent()) {
            // 更新操作
            StudentInfoApprove studentInfoApprove = new StudentInfoApprove();
            studentInfoApprove.setContent(data);
            studentInfoApprove.setStudentId(bsssStudentGraduate.getStudentId());
            studentInfoApprove.setOutsideType("2");
            studentInfoApprove.setApprovalStatus(0);
            studentInfoApprove.setName(bsssStudentGraduate.getName());
            studentInfoApprove.setIdCardNumber(bsssStudentGraduate.getIdCardNumber());
            studentInfoApprove.setFlag(bsssStudentGraduate.getFlag());
            studentInfoApproveMapper.updateStudentInfoApprove(studentInfoApprove);
        } else {
            // 插入操作
            StudentInfoApprove studentInfoApprove = createNewStudentInfoApprove(data,bsssStudentGraduate);
            studentInfoApprove.setOutsideType("2");
            studentInfoApprove.setApprovalStatus(0);
            studentInfoApproveMapper.insertStudentInfoApprove(studentInfoApprove);
        }
        if(graduate.isPresent()){
            wrapper.eq(BsssStudentGraduate::getStudentId, bsssStudentGraduate.getStudentId());
            return wrapper.update();
        }else {
            BsssStudentGraduate entity = new BsssStudentGraduate();
            BeanUtils.copyProperties(bsssStudentGraduate, entity);
            bsssStudentGraduateMapper.insert(entity);
            return true;
        }
        //todo 后期新增学籍变更信息 字段 ↓
        //根据id修改

    }

    // 生成 VerifyResponse 的 JSON 数据
    private String generateVerifyResponseJson(BsssStudentGraduateVo bsssStudentGraduate) {
        try {
            VerifyResponse verifyResponse = new VerifyResponse();
            if(bsssStudentGraduate.getBirthDate() != null){
                     SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
                String birthDateStr = sdf.format(bsssStudentGraduate.getBirthDate());
                verifyResponse.setBirthDate(birthDateStr);
            }
            BeanUtils.copyProperties(bsssStudentGraduate, verifyResponse);
            verifyResponse.setLearningSituation(bsssStudentGraduate.getLearningSituation());
            verifyResponse.setForeignLanguage(bsssStudentGraduate.getForeignLanguage());
            verifyResponse.setPracticeUnit(bsssStudentGraduate.getPracticeUnit());
            verifyResponse.setLearningContent(bsssStudentGraduate.getLearningContent());
            verifyResponse.setThesisTitle(bsssStudentGraduate.getThesisTitle());
            verifyResponse.setGraduationDesign(bsssStudentGraduate.getGraduationDesign());

            // JSON 序列化
            ObjectMapper mapper = new ObjectMapper();
            return mapper.writeValueAsString(verifyResponse);
        } catch (JsonProcessingException e) {
            throw new RuntimeException("JSON 序列化失败", e);
        }
    }
    // 创建新记录对象
    private StudentInfoApprove createNewStudentInfoApprove(String data, BsssStudentGraduateVo bsssStudentGraduate) {
        StudentInfoApprove studentInfoApprove = new StudentInfoApprove();
        studentInfoApprove.setStudentId(bsssStudentGraduate.getStudentId());
        studentInfoApprove.setContent(data);
        studentInfoApprove.setOfficeId(bsssStudentGraduate.getOfficeId());
        studentInfoApprove.setFlag(bsssStudentGraduate.getFlag());
        studentInfoApprove.setApprovalStatus(0);
        return studentInfoApprove;
    }
}
